/*     */ package com.chw.utils;
/*     */ 
/*     */ import java.security.MessageDigest;
/*     */ import org.apache.commons.codec.digest.DigestUtils;
/*     */ 
/*     */ public class Md5EncryptUtil
/*     */ {
/*     */   private static final String MD5_SALT_VALUE = "WlRFSUNU5YWo5Z+f5peF5ri4572RU0FMVA==";
/*  20 */   private static final ThreadLocal<Md5EncryptUtil> local = new ThreadLocal();
/*  21 */   private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
/*     */ 
/*     */   public static Md5EncryptUtil getEncrypt()
/*     */   {
/*  29 */     Md5EncryptUtil encrypt = (Md5EncryptUtil)local.get();
/*  30 */     if (encrypt == null) {
/*  31 */       encrypt = new Md5EncryptUtil();
/*  32 */       local.set(encrypt);
/*     */     }
/*  34 */     return encrypt;
/*     */   }
/*     */ 
/*     */   private static String byteArrayToHexString(byte[] b) {
/*  38 */     StringBuffer resultSb = new StringBuffer();
/*  39 */     for (int i = 0; i < b.length; i++) {
/*  40 */       resultSb.append(byteToHexString(b[i]));
/*     */     }
/*  42 */     return resultSb.toString();
/*     */   }
/*     */ 
/*     */   private static String byteToHexString(byte b) {
/*  46 */     int n = b;
/*  47 */     if (n < 0)
/*  48 */       n += 256;
/*  49 */     int d1 = n / 16;
/*  50 */     int d2 = n % 16;
/*  51 */     return hexDigits[d1] + hexDigits[d2];
/*     */   }
/*     */ 
/*     */   public static String MD5Encode(String origin, String charsetname)
/*     */   {
/*  62 */     String resultString = null;
/*     */     try {
/*  64 */       resultString = new String(origin);
/*  65 */       MessageDigest md = MessageDigest.getInstance("MD5");
/*  66 */       if ((charsetname == null) || ("".equals(charsetname)))
/*  67 */         resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
/*     */       else
/*  69 */         resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
/*     */     } catch (Exception exception) {
/*     */     }
/*  72 */     return resultString;
/*     */   }
/*     */ 
/*     */   public static String encode(String key)
/*     */   {
/*  82 */     return encode(key, false);
/*     */   }
/*     */ 
/*     */   public static String encode(String key, boolean saltValueFlag)
/*     */   {
/*  95 */     if (key == null) {
/*  96 */       return null;
/*     */     }
/*     */ 
/*  99 */     if (saltValueFlag) {
/* 100 */       key = "WlRFSUNU5YWo5Z+f5peF5ri4572RU0FMVA==" + key;
/*     */     }
/* 102 */     return DigestUtils.md5Hex(key);
/*     */   }
/*     */ }

